package com.whsr.readservice.dao;

import com.whsr.readservice.vo.DownBookMaintain;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface DownBookMaintainMapper {

    @Select("select" +
            " book_id as bookId " +
            "from down_book_maintain " +
            "where status = #{status}")
    List<Integer> selectBookIdsByStatus(@Param("status") Integer status);

    @Select("select" +
            " book_id as bookId " +
            "from down_book_maintain " +
            "where status != #{status}")
    List<Integer> selectBookIdsByNoStatus(@Param("status") Integer status);

    @Select("select" +
            " count(1) " +
            "from down_book_maintain " +
            "where book_id = #{bookId}")
    Integer selectCountByBookId(@Param("bookId") Integer bookId);

    @Insert("<script>" +
            "insert into down_book_maintain (book_id, status" +
            " create_time, update_time)" +
            "values" +
            "<foreach collection =\"downBookMaintains\" item=\"downBookMaintain\" index=\"index\" separator =\",\">" +
            "( #{downBookMaintain.bookId}, #{downBookMaintain.status}, " +
            "  now(), now())" +
            "</foreach>" +
            "</script>")
    int insertDownBookMaintains(@Param("downBookMaintains") List<DownBookMaintain> downBookMaintains);


    @Update("<script>" +
            "update down_book_maintain " +
            "set " +
            " status = #{status}, " +
            " update_time = now() " +
            "where book_id in " +
            "<foreach collection=\"bookIds\" index=\"index\" item=\"bookId\" separator=\",\" open=\"(\" close=\")\">" +
            "#{bookId}" +
            "</foreach>" +
            "</script>")
    int updateDownBookMaintains(@Param("bookIds") List<Integer> bookIds, @Param("status") Integer status);
}
